var app = getApp();
var webhost = app.globalData.webhost;
var util = require('../../utils/util.js');
var utilDate = require('../../utils/date.js');

Page({
  data: {
    dateStart: '出车日期起',
    dateEnd: '止',
    pageNumber: 1,
    pageSize: 5,
    a1: '',
    a2: '',
    dataList: [],
    sum: 0,
    selectList: [],
    selectedAllStatus: false,
    planIdList: [],
    disabled: false,
    loading: 0
  },

  bindStartChange: function(e) {
    var that = this;
    var date = e.detail.value;
    that.setData({
      dateStart: date
    })
  },


  bindEndChange: function(e) {
    var that = this;
    var date = e.detail.value;
    that.setData({
      dateEnd: date
    })
  },

  bindSelectAll: function() {
    var selectList = this.data.selectList;
    var selectedAllStatus = this.data.selectedAllStatus;
    for (var i = 0; i < selectList.length; i++) {
      selectList[i] = !selectedAllStatus
    }
    this.setData({
      selectList: selectList,
      selectedAllStatus: !selectedAllStatus
    })
    this.sum();
  },

  bindChange_a1: function(e) {
    var value = e.detail.value;
    this.setData({
      a1: value
    })
  },

  bindChange_a2: function(e) {
    var value = e.detail.value;
    this.setData({
      a2: value
    })
  },

  bindSearch: function() {
    this.setData({
      selectList:[],
      dataList:[],
      pageNumber:1,
      sum:0,
      loading:0,
      selectedAllStatus: false,
    })
    this.queryPlanForApplyReim()
  },

  bindCheckbox: function(e) {
    var index = +e.currentTarget.dataset.index;
    var sum = this.data.sum;
    var selectList = this.data.selectList;
    selectList[index] = !selectList[index];
    var selectedAllStatus = this.data.selectedAllStatus;
    this.setData({
      selectList: selectList
    })
    this.sum();
  },

  sum: function() {
    var sum = 0;
    var selectList = this.data.selectList;
    for (var i = 0; i < selectList.length; i++) {
      if (selectList[i]) {
        sum += 1
      }
    }
    this.setData({
      sum: sum,
      disabled: sum == 0 ? false : true,
    })
    if (sum > 0 && sum == selectList.length) {
      this.setData({
        selectedAllStatus: true
      })
    } else {
      this.setData({
        selectedAllStatus: false
      })
    }
  },

  //判断所选配载单是否为同一车号、同一主驾、同一副驾
  judgeTheSame: function() {
    var that = this;
    var selectList = that.data.selectList;
    var dataList = that.data.dataList;
    var flag = true;
    for (var i = 0; i < selectList.length - 1; i++) {
      if (selectList[i]) {
        for (var j = i + 1; j < selectList.length; j++) {
          if (selectList[j]) {
            if (dataList[i].carCode != dataList[j].carCode || dataList[i].n1 != dataList[j].n1 || dataList[i].n2 != dataList[j].n2) {
              flag = false;
              wx.showModal({
                title: '无法报账',
                content: '所选配载单信息不符合车牌号、主驾、副驾需一致的报账原则，无法申请报账',
                showCancel: false,
              })
              break;
            }
          }
        }
      }
    }
    return flag;
  },

  queryPlanForApplyReim: function() {
    var that = this;
    //设定初始查询时间间隔为 当前时间前一周到当前时间
    var myDate = new Date();
    var myDate2 = new Date(myDate.getTime() - 90 * 24 * 3600 * 1000);
    var date = utilDate.Format('yyyy-MM-dd', myDate);
    var date2 = utilDate.Format('yyyy-MM-dd', myDate2);
    var disabled = that.data.disabled;
    var dateStart = that.data.dateStart == '出车日期起' ? date2 : that.data.dateStart;
    var dateEnd = that.data.dateEnd == '止' ? date : that.data.dateEnd;
    var parameters = [{
      String: {
        driverAccount: app.globalData.driverAccount,
        sessionid: app.globalData.sessionid,
        dateStart: dateStart,
        dateEnd: dateEnd,
        pageNumber: that.data.pageNumber,
        pageSize: that.data.pageSize,
        a1: that.data.a1,
        a2: that.data.a2,
      }
    }];
    wx.request({
      url: webhost,
      data: {
        eap_username: "driver",
        eap_password: "Driver2018",
        boId: "tspdriver_tsp2DriverServiceBO_bo", //调用的bo
        methodName: 'queryPlanForApplyReim', //调用的具体方法
        returnType: "json", //返回参数类型
        parameters: JSON.stringify(parameters)
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      method: 'POST',
      success: function({data}) {
        if(data.code && data.code == '000000'){
          var dataList = data.dataList;
          var selectList = new Array();
          if (dataList.length > 0) {
            for (var i = 0; i < dataList.length; i++) {
              selectList[i] = false;
              if (dataList[i].sCreateDate){
                dataList[i].sCreateDate = dataList[i].sCreateDate.substring(0, 10)
              }
              for (var key in dataList[i]) {
                if (dataList[i][key] == null) {
                  dataList[i][key] = ''
                }
              }
            }
            that.setData({
              dataList: that.data.dataList.concat(dataList),
              selectList: that.data.selectList.concat(selectList),
              loading: 0,
              selectedAllStatus: false,
            })
            //当 selectedList中没有true值时，要将‘申请报账’按钮置位不可用
            if (that.data.selectList.indexOf(true) == -1){
              that.setData({
                disabled: false
              })
            }

          } else {
            that.setData({
              loading: 2
            })
          }
        }
      },
      complete: function() {
        that.setData({
          // disabled: false
        })
      }
    })
  },

  bindApplyReim: function() {
    var that = this;
    var selectList = that.data.selectList;
    var dataList = that.data.dataList;
    var planIdList = [];
    var planId = '';
    for (var i = 0; i < selectList.length; i++) {
      if (selectList[i]) {
        planIdList.push({
          planId: dataList[i].planId
        })
      }
    }
    var parameters = [{
      String: {
        driverAccount: app.globalData.driverAccount,
        sessionid: app.globalData.sessionid,
        dataList: planIdList
      }
    }];
    var disabled = that.data.disabled;
    that.setData({
      disabled: true
    })
    if (planIdList[0] != undefined) {
      if (that.judgeTheSame()) {
        wx.request({
          url: webhost,
          data: {
            eap_username: "driver",
            eap_password: "Driver2018",
            boId: "tspdriver_tsp2DriverServiceBO_bo", //调用的bo
            methodName: 'applyReimEvent', //调用的具体方法
            returnType: "json", //返回参数类型
            parameters: JSON.stringify(parameters)
          },
          header: {
            'content-type': 'application/x-www-form-urlencoded'
          },
          method: 'POST',
          success: function(res) {
            var code = res.data.code;
            if(code && code == '000000'){
              wx.showToast({
                title: '申请成功',
              })
              that.setData({
                selectList: [],
                dataList: [],
                pageNumber: 1,
                sum: 0,
                loading: 0,
                selectedAllStatus: false,
              })
              that.queryPlanForApplyReim()
            } else {
              wx.showModal({
                title: '提示',
                showCancel: false,
                confirmColor: '#118EDE',
                content: res.data.msg,
                success: function (res) {
                  if (code == '003000') {
                    if (res.confirm) {
                      wx.reLaunch({
                        url: '../login/login'
                      })
                    }
                  }
                }
              })
            }
          }
          
        })
      } else {
        that.setData({
          disabled: false
        })
      }
    } else {
      wx.showToast({
        title: '未选择配载单',
        icon: 'loading',
        mask: true,
        duration: 1000
      })
      that.setData({
        disabled: false
      })
    }
  },

  onLoad: function(options) {
    var that = this;

    that.queryPlanForApplyReim();

  },

  onShow: function() {

  },

  onPullDownRefresh: function() {
    setTimeout(() => {
      wx.stopPullDownRefresh();
    }, 1500);
  },

  onReachBottom: function() {
    var that = this;
    if (that.data.loading == 0) {
      that.setData({
        loading: true,
        pageNumber: that.data.pageNumber + 1
      })
      setTimeout(() => {
        that.queryPlanForApplyReim();
      }, 300);
    }
  },


})